package activator;

import java.util.Dictionary;
import java.util.Hashtable;

import library.service.LibraryService;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.cm.ManagedService;

import service.SciFiLibraryServiceImpl;

public class Activator implements BundleActivator {
	private static final String LIB_SERVICE_PID = "library.service.pid";
	ServiceRegistration libAndManagedServiceReg;

	public void start(BundleContext context) throws Exception {
		// service object creation
		// It is a ManagedService and LibraryService at the same time.
		SciFiLibraryServiceImpl libAndManagedService = new SciFiLibraryServiceImpl();

		// Prepare the mandatory ManagedService's props
		Dictionary managedServiceProps = new Hashtable(2);
		managedServiceProps.put(Constants.SERVICE_PID, LIB_SERVICE_PID);
		managedServiceProps.put("test", "ala-bala");

		// register LibraryService and ManagedService where:
		// 1) use the common service object
		// 2) apply managedServiceProps
		String[] serviceClasses = new String[] {
				LibraryService.class.getName(), ManagedService.class.getName() };
		libAndManagedServiceReg = context.registerService(serviceClasses,
				libAndManagedService, managedServiceProps);

		libAndManagedService = null;
	}

	public void stop(BundleContext context) throws Exception {
		if (libAndManagedServiceReg != null) {
			libAndManagedServiceReg.unregister();
			libAndManagedServiceReg = null;
		}
	}
}
